package cn.shujuhai.cms.controller.article;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.shujuhai.cms.domain.InfoArticle;
import cn.shujuhai.cms.service.InfoArticleService;
import cn.shujuhai.common.aop.Idempotent;
import cn.shujuhai.common.aop.SysLog;
import cn.shujuhai.common.domain.PageRequest;
import cn.shujuhai.common.pluging.data.ResultVo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Author: zhangDeZhong
 * @Date: 2024/12/5 12:01
 * @Description: 提供表【info_article(信息文章)】的操作接口
 **/
@RestController
@RequestMapping("/cms/infoArticle")
@RequiredArgsConstructor
public class InfoArticleController {

    final InfoArticleService infoArticleService;

    /**
     * 根据栏目ID和分页查询参数获取文章列表
     *
     * @param channelId   栏目ID
     * @param title       搜索关键字（栏目标题）
     * @param pageRequest
     * @return 文章列表的分页结果
     */
    @SysLog(value = "文章列表查询", operationType = "查询", module = "文章模块")
    @GetMapping("/list")
    public ResultVo<?> getArticlesByTitle(
            @RequestParam Long channelId,
            @RequestParam(defaultValue = "") String title,
            @Valid PageRequest pageRequest) {
        return this.infoArticleService.getArticlesByTitle(channelId, title, pageRequest.getPageNum(), pageRequest.getPageSize());
    }

    /**
     * 添加文章信息
     *
     * @param infoArticle 文章信息
     * @return 结果信息
     */
    @SysLog(value = "添加文章", operationType = "新增", module = "文章模块")
    @PostMapping("/add")
    @Idempotent
    public ResultVo<?> addArticle(@RequestBody @Validated InfoArticle infoArticle) {
        return this.infoArticleService.addArticle(infoArticle);
    }

    /**
     * 修改文章信息
     *
     * @param infoArticle 文章信息
     * @return 结果信息
     */
    @SysLog(value = "修改文章", operationType = "修改", module = "文章模块")
    @PostMapping("/update")
    public ResultVo<?> updateArticle(@RequestBody @Validated InfoArticle infoArticle) {
        return this.infoArticleService.updateArticle(infoArticle);
    }

    /**
     * 根据文章ID删除文章信息
     *
     * @param articleId 文章ID
     * @return 结果信息
     */
    @SysLog(value = "删除文章", operationType = "删除", module = "文章模块")
    @PostMapping("/delete/{articleId}")
    public ResultVo<?> removeArticle(@PathVariable Long articleId) {
        return this.infoArticleService.removeArticle(articleId);
    }

    /**
     * 根据文章ID列表批量删除文章信息
     *
     * @param articleIds 文章ID列表
     * @return 结果信息
     */
    @SysLog(value = "删除文章", operationType = "批量删除", module = "文章模块")
    @SaCheckPermission("infoArticle_delete")
    @PostMapping("/removeBatch")
    public ResultVo<?> removeBatchArticle(@RequestParam List<Long> articleIds) {
        return this.infoArticleService.removeBatchArticle(articleIds);
    }

    /**
     * 根据文章ID获取文章内容
     *
     * @param articleId 文章ID
     * @return 文章内容结果
     */
    @SaCheckPermission("infoArticle_list")
    @GetMapping("/getContentById")
    public ResultVo<?> getContentById(@RequestParam Long articleId) {
        return this.infoArticleService.getContentById(articleId);
    }

    /**
     * 根据文章ID获取文章信息
     *
     * @param articleId 文章ID
     * @return 文章信息结果
     */
    @SaCheckPermission("infoArticle_enable")
    @GetMapping("/getInfoById")
    public ResultVo<?> getInfoById(@RequestParam Long articleId) {
        return this.infoArticleService.getInfoById(articleId);
    }

    /**
     * 修改文章发布状态信息（审核）
     *
     * @param articleId 文章ID
     * @param status    文章发布状态(待审核:0,审核通过:1,被驳回:2)
     * @return 结果信息
     */
    @SysLog(value = "审核文章", operationType = "审核", module = "文章模块")
    @SaCheckPermission("infoArticle_enable")
    @PostMapping("/updateStatus")
    public ResultVo<?> updateStatus(@RequestParam Long articleId, @RequestParam Integer status, @RequestParam String reason) {
        return this.infoArticleService.updateStatus(articleId, status, reason);
    }


    /**
     * 文章头图上传
     *
     * @param file 文件内容
     * @return 结果信息
     */
    @SysLog(value = "文章头图上传", operationType = "上传文件", module = "文章模块")
    @SaCheckPermission("infoArticle_list")
    @PostMapping("/uploadImg")
    public ResultVo<?> uploadImg(@RequestParam("file") MultipartFile file) {
        return this.infoArticleService.uploadImg(file);
    }

    /**
     * 文章附件上传
     *
     * @param file 文件内容
     * @return 结果信息
     */
    @SysLog(value = "文章附件上传", operationType = "上传文件", module = "文章模块")
    @SaCheckPermission("infoArticle_list")
    @PostMapping("/uploadAttachment")
    public ResultVo<?> uploadAttachment(@RequestParam("file") MultipartFile file) {
        return this.infoArticleService.uploadAttachment(file);
    }

    /**
     * 视频文件上传
     *
     * @param file 文件内容
     * @return 结果信息
     */
    @SysLog(value = "视频文件上传", operationType = "上传文件", module = "文章模块")
    @SaCheckPermission("infoArticle_list")
    @PostMapping("/uploadVideo")
    public ResultVo<?> uploadVideo(@RequestParam("file") MultipartFile file) {
        return this.infoArticleService.uploadVideo(file);
    }

    /**
     * 获取总信息流量信息
     * 此方法用于获取网站或应用中所有文章的总浏览量
     * 主要用于统计和展示平台的总体访问情况
     *
     * @return 返回一个ResultVo对象，其中包含总浏览量的信息
     */
    @SaCheckLogin
    @GetMapping("/totalViewCount")
    public ResultVo<?> getTotalViewCount() {
        return this.infoArticleService.getTotalViewCount();
    }


    @SaCheckLogin
    @GetMapping("/viewArticleRanking")
    public ResultVo<?> viewArticleRanking() {
        return this.infoArticleService.viewArticleRanking();
    }


    /**
     * 根据栏目ID获取文章列表
     *
     * @param channelId 栏目ID
     * @return 文章列表结果信息
     */
    @SaCheckPermission("email_list")
    @GetMapping("/getArticlesByChannelId")
    public ResultVo<?> getArticlesByChannelId(@RequestParam Long channelId) {
        return this.infoArticleService.getArticlesByChannelId(channelId);
    }


    /**
     * 全文检索
     *
     * @param search   搜索关键字
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 文章列表的分页结果
     */
    @SysLog(value = "全文检索", operationType = "查询", module = "文章模块")
    @GetMapping("/search_list")
    public ResultVo<?> search_list(
            @RequestParam(defaultValue = "") String search,
            @RequestParam int pageNum,
            @RequestParam int pageSize) {
        return this.infoArticleService.search_list(search, pageNum, pageSize);
    }

    @PostMapping("/countArticleNum")
    public void countArticleNum(@RequestBody Map<String, String> body) {
        this.infoArticleService.countArticleNum(body);
    }

    @GetMapping("countArticle")
    public ResultVo<?> getCountArticle(String date) {
        return this.infoArticleService.getCountArticle(date);
    }


}
